﻿layui.config({
    base: '/Lib/layui/extend/'
}).extend({
    formSelects: 'formSelects-v4'
}).use(['form', 'layer', 'table', 'laypage', 'laydate', 'jquery', 'formSelects'], function () {
    var form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        player = parent.layer || layui.layer,
        formSelects = layui.formSelects; //很重要;

    var staffEmployeeListLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                ac: new AsyncCaller(),    //异步方法管理
                sDepartmentCode: "",
                field: 'Name',
                order: 'asc',
                projectAllList: "/Project/Info/LoadProjectAllList",

                listUrl: "/Staff/Employee/LoadEmployeeList",
                exportInfoUrl: "/Staff/Employee/EpxortEmployee",
                modifyUrl: "/Staff/Employee/Modify",
                forbiddenUrl: "/Staff/Employee/Forbidden",
                removeUrl: "/Staff/Employee/Remove",
            };

            this.Parameters.ac
                //.pushQueue(function () {
                //    CommonLogic.loadButtonsInfo(function () {
                //        self.Parameters.ac.notifyQueueAsyncFuncComplate();
                //    })
                //})
                .pushQueue(function () {
                    self.logicFunc.loadProjectAllList(function () {
                        self.Parameters.ac.notifyQueueAsyncFuncComplate();
                    });
                })
                .pushQueue(function () {
                    self.initPage();
                    self.Parameters.ac.notifyQueueAsyncFuncComplate();
                })
                .pushQueue(this.bindEvent)
                .exec();

            //this.initPage();
            //this.bindEvent();
        },
        //初始化列表
        initPage: function () {

            //获取用户信息
            var title =
                [{ type: 'checkbox', fixed: 'left' },
                {
                    field: 'Rnumber', title: '编号', width: 60, align: "center", fixed: 'left',
                    templet: function (d) {
                        //每页都从1开始 return d.LAY_INDEX; //只有第一页才从1开始，（相当于数据表的所有记录数的下标。）
                        return d.LAY_INDEX;
                    }
                },
                { field: 'ID', title: 'ID', hide: true },
                { field: 'ZTBS', title: 'ZTBS', hide: true },
                { field: 'Name', title: '姓名', width: 130, align: "center", sort: true, fixed: 'left' },
                { field: 'UserNo', title: '编号', width: 170, align: "center", sort: true, fixed: 'left' },
                {
                    field: 'Sex', title: '性别', width: 80, align: "center", fixed: 'left',
                    templet: function (d) {
                        return GrdEnum.getValue.userSexValue(d.Sex.trim());
                    }
                },
                { field: 'ProjectName', title: '项目名称', width: 120, align: "center", fixed: 'left' },
                {
                    field: 'Credit', title: '信誉', width: 70, align: "center",
                    templet: function (d) {
                        return GrdEnum.getValue.creditTypeValue(d.Credit);
                    }
                },
                { field: 'Code', title: '身份证号', width: 150, align: "center" },
                { field: 'OrgaizeName', title: '组织机构', width: 150, align: "center" },
                { field: 'Post', title: '职务', width: 150, align: "center" },
                { field: 'WorkType', title: '工种', width: 150, align: "center" },
                { field: 'MName', title: '类别', width: 120, align: "center" },
                { field: 'MonthMoney', title: '月薪工资', width: 100, align: "center" },
                { field: 'PostMoney', title: '岗位工资', width: 100, align: "center" },
                { field: 'SocialMoney', title: '社保工资', width: 100, align: "center" },
                { field: 'HouseMoney', title: '公积金', width: 100, align: "center" },
                { field: 'DayMoney', title: '日薪', width: 120, align: "center" },
                { field: 'OverMoney', title: '小时工资', width: 120, align: "center" },
                { field: 'BankNo', title: '银行账号', width: 120, align: "center" },
                { field: 'BankName', title: '开户银行', width: 120, align: "center" },
                { field: 'Phone', title: '联系电话', width: 120, align: "center" },
                { field: 'CrashPerson', title: '家属', width: 120, align: "center" },
                { field: 'CrashPhone', title: '家属电话', width: 120, align: "center" },
                {
                    field: 'WorkINDate', title: '入职时间', width: 120, align: "center",
                    templet: function (d) {
                        return GrdAssist.formatDate(d.WorkINDate, "yyyy-MM-dd");
                    }
                },
                { field: 'is_valid', title: '在职状态', width: 100, templet: '#switchZTBSTpl', align: "center", fixed: 'right' },
                { field: 'opt', title: '操作', toolbar: '#gridTool', width: '180', align: 'left', fixed: 'right' }
                ];

            //table加载
            GrdLayUI.table({
                dom: '#InfoList',
                height: "full-140",
                url: self.Parameters.listUrl,
                field: self.Parameters.field,
                order: self.Parameters.order,
                parameters: {
                    CheckZT: 1
                },
                title: title
            });

            //排序监听
            GrdLayUI.tableSort({
                domId: "InfoList",
                backAction: function (obj) {
                    self.Parameters.field = obj.field;
                    self.Parameters.order = obj.type;
                    self.logicFunc.loadListInfo();
                }
            });
            form.render();
        },
        //绑定事件
        bindEvent: function () {

            //监听(列表操作)
            table.on('tool(InfoList)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case "funBtnEdit":
                        self.logicFunc.modifyInfo(data);
                        break;
                    case "funBtnRemove":
                        self.logicFunc.removeInfo(data);
                        break;
                }
            });

            //查询
            $("#funBtnSearch").on("click", function () {
                self.logicFunc.loadListInfo();
            });

            //修改
            $("#funBtnEdit").on("click", function () {
                self.logicFunc.modifyInfo();
            });

            //导出
            $("#funBtnExport").on("click", function () {
                self.logicFunc.exportInfo();
            });

            //删除
            $("#funBtnRemove").on("click", function () {
                self.logicFunc.removeInfo();
            });

            //监听启用/停用操作
            form.on('switch(funBtnZTBS)', function (obj) {

                self.logicFunc.forbiddenInfo(this.value);

            });

        },
        /*
         * 逻辑方法
         */
        logicFunc: {

            //获取所有的工程信息
            loadProjectAllList: function (func) {
                GrdLayUI.ajaxPost({
                    url: self.Parameters.projectAllList,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            $("#ProjectNo").empty();
                            $("#ProjectNo").append("<option value=''>所有工程项目</option>");
                            for (var i = 0; i < result.Data.length; i++) {
                                $("#ProjectNo").append("<option value=" + result.Data[i].ProjectNo + ">" + result.Data[i].ProjectName + "</option>");
                            }
                        }
                        if (func) {
                            func();
                        }
                    }
                });
            },

            ///获取数据
            loadListInfo: function () {
                GrdLayUI.tableReload({
                    domId: "InfoList",
                    field: self.Parameters.field,
                    order: self.Parameters.order,
                    parameters: {
                        ProjectNo: $("#ProjectNo").val(),
                        KeyWord: $("#KeyWord").val(),
                        ZTBS: $("#ZTBS").val(),
                        MID: $("#MID").val(),
                        CheckZT: 1
                    }
                });
            },

            //修改信息
            modifyInfo: function (data) {

                if (data != undefined && data != null) {
                    var JsonData = {
                        ID: data.ID,
                        Name: data.Name,
                        UserNo: data.UserNo,
                        Sex: data.Sex.trim(),
                        ProjectNo: data.ProjectNo.trim(),
                        ProjectName: data.ProjectName,
                        Credit: data.Credit,
                        Code: data.Code,
                        OrgaizeName: data.OrgaizeName,
                        Post: data.Post,
                        ZTBS: data.ZTBS,
                        WorkType: data.WorkType,
                        MName: data.MName,
                        MID: data.MID,
                        MonthMoney: data.MonthMoney,
                        PostMoney: data.PostMoney,
                        SocialMoney: data.SocialMoney,
                        HouseMoney: data.HouseMoney,
                        DayMoney: data.DayMoney,
                        OverMoney: data.OverMoney,
                        BankNo: data.BankNo,
                        BankName: data.BankName,
                        Phone: data.Phone,
                        CrashPerson: data.CrashPerson,
                        CrashPhone: data.CrashPhone,
                        Address: data.Address,
                        Descrption: data.Descrption
                    };
                    var index = layui.layer.open({
                        title: "修改信息",
                        type: 2,
                        resize: false,
                        area: ['700px', '700px'],
                        content: self.Parameters.modifyUrl + '?data=' + GrdLayUI.zip(JsonData),
                        end: function () {
                            $(window).unbind("resize");
                        }
                    });
                    layui.layer.full(index);
                } else {
                    GrdLayUI.getSingleCheckData({
                        domId: "InfoList",
                        backAction: function (data) {
                            var JsonData = {
                                ID: data.ID,
                                Name: data.Name,
                                UserNo: data.UserNo,
                                Sex: data.Sex.trim(),
                                ProjectNo: data.ProjectNo.trim(),
                                ProjectName: data.ProjectName,
                                Credit: data.Credit,
                                Code: data.Code,
                                OrgaizeName: data.OrgaizeName,
                                Post: data.Post,
                                ZTBS: data.ZTBS,
                                WorkType: data.WorkType,
                                MName: data.MName,
                                MID: data.MID,
                                MonthMoney: data.MonthMoney,
                                PostMoney: data.PostMoney,
                                SocialMoney: data.SocialMoney,
                                HouseMoney: data.HouseMoney,
                                DayMoney: data.DayMoney,
                                OverMoney: data.OverMoney,
                                BankNo: data.BankNo,
                                BankName: data.BankName,
                                Phone: data.Phone,
                                CrashPerson: data.CrashPerson,
                                CrashPhone: data.CrashPhone,
                                Address: data.Address,
                                Descrption: data.Descrption
                            };
                            var index = layui.layer.open({
                                title: "修改信息",
                                type: 2,
                                resize: false,
                                area: ['700px', '700px'],
                                content: self.Parameters.modifyUrl + '?data=' + GrdLayUI.zip(JsonData),
                                end: function () {
                                    $(window).unbind("resize");
                                }
                            });
                            layui.layer.full(index);
                        }
                    });

                }
            },

            //导出信息
            exportInfo: function () {
                var json = {
                    parameters: {
                        ProjectNo: $("#ProjectNo").val(),
                        WorkType: $("#WorkType").val(),
                        ZTBS: $("#ZTBS").val(),
                        Name: $("#Name").val(),
                        PYM: $("#PYM").val(),
                        OrgaizeName: $("#OrgaizeName").val(),
                        CheckZT: 1
                    }
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.exportInfoUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {
                            window.open("http://" + location.host + result.Msg, "_self");
                        }
                    }
                });
            },

            //启用或停用
            forbiddenInfo: function (id) {
                var json = {
                    parameters: id
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.forbiddenUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {
                            GrdLayUI.msg(result.Msg, "成功", function () {
                                //self.logicFunc.loadListInfo();
                            });
                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },


            //删除信息
            removeInfo: function (data) {
                if (data != undefined && data != null) {
                    GrdLayUI.removeDataByIdInfo({
                        data: data,
                        removeUrl: self.Parameters.removeUrl,
                        backAction: function (data) {
                            self.logicFunc.loadListInfo();
                        }
                    });
                } else {
                    GrdLayUI.removeDataById({
                        domId: "InfoList",
                        removeUrl: self.Parameters.removeUrl,
                        backAction: function (data) {
                            self.logicFunc.loadListInfo();
                        }
                    });
                }
            },
        },
        //工具
        tools: {

        }
    };
    staffEmployeeListLogic.init();
});
